package pokerclient.view.panels;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;

import pokerclient.ctrl.ClientController;

import javax.swing.JTextField;
import javax.swing.LayoutStyle;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ActionPanel extends javax.swing.JPanel {
	private static final long serialVersionUID = -667280534731615734L;
	private JButton checkButton;
	private JButton callButton;
	private JTextField betTextField;
	private JButton betButton;
	private JButton allinButton;
	private JButton foldButton;

	/**
	 * Auto-generated main method to display this JPanel inside a new JFrame.
	 */

	public ActionPanel() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent) this);
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(576, 56));
			{
				checkButton = new JButton();
				checkButton.setName("checkButton");
				checkButton.setAction(getAppActionMap().get("checkAction"));
				checkButton.setEnabled(false);
			}
			{
				callButton = new JButton();
				callButton.setName("callButton");
				callButton.setAction(getAppActionMap().get("callAction"));
				callButton.setEnabled(false);
			}
			{
				foldButton = new JButton();
				foldButton.setName("foldButton");
				foldButton.setAction(getAppActionMap().get("foldAction"));
				foldButton.setEnabled(false);
			}
			{
				allinButton = new JButton();
				allinButton.setName("allinButton");
				allinButton.setAction(getAppActionMap().get("allinAction"));
				allinButton.setEnabled(false);
			}
			{
				betButton = new JButton();
				betButton.setName("betButton");
				betButton.setAction(getAppActionMap().get("betAction"));
				betButton.setEnabled(false);
			}
			{
				betTextField = new JTextField();
				betTextField.setName("betTextField");
				betTextField.setEnabled(false);
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
					.addContainerGap().addGroup(
							thisLayout.createParallelGroup(
									GroupLayout.Alignment.BASELINE)
									.addComponent(checkButton,
											GroupLayout.Alignment.BASELINE, 0,
											32, Short.MAX_VALUE).addComponent(
											callButton,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE, 32,
											GroupLayout.PREFERRED_SIZE)
									.addComponent(foldButton,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE, 32,
											GroupLayout.PREFERRED_SIZE)
									.addComponent(allinButton,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE, 32,
											GroupLayout.PREFERRED_SIZE)
									.addComponent(betButton,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE, 32,
											GroupLayout.PREFERRED_SIZE)
									.addComponent(betTextField,
											GroupLayout.Alignment.BASELINE, 0,
											27, Short.MAX_VALUE))
					.addContainerGap());
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
					.addComponent(checkButton, GroupLayout.PREFERRED_SIZE, 74,
							GroupLayout.PREFERRED_SIZE).addPreferredGap(
							LayoutStyle.ComponentPlacement.UNRELATED)
					.addComponent(callButton, GroupLayout.PREFERRED_SIZE, 74,
							GroupLayout.PREFERRED_SIZE).addGap(0, 17,
							Short.MAX_VALUE).addComponent(betTextField,
							GroupLayout.PREFERRED_SIZE, 104,
							GroupLayout.PREFERRED_SIZE).addPreferredGap(
							LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(betButton, GroupLayout.PREFERRED_SIZE, 74,
							GroupLayout.PREFERRED_SIZE).addPreferredGap(
							LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(allinButton, GroupLayout.PREFERRED_SIZE, 74,
							GroupLayout.PREFERRED_SIZE).addGap(52)
					.addComponent(foldButton, GroupLayout.PREFERRED_SIZE, 74,
							GroupLayout.PREFERRED_SIZE).addContainerGap());
			Application.getInstance().getContext().getResourceMap(getClass())
					.injectComponents(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Action
	public void foldAction() {
		ClientController.getInstance().foldButtonPressed();
	}

	/**
	 * Returns the action map used by this application. Actions defined using
	 * the Action annotation are returned by this method
	 */
	private ApplicationActionMap getAppActionMap() {
		return Application.getInstance().getContext().getActionMap(this);
	}

	@Action
	public void betAction() {
		try {
			int bet = Integer.parseInt(betTextField.getText());
			ClientController.getInstance().betButtonPressed(bet);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null,
					"Wrong input. Only integers allowed.");
		}
	}

	@Action
	public void checkAction() {
		ClientController.getInstance().checkButtonPressed();
	}

	@Action
	public void callAction() {
		ClientController.getInstance().callButtonPressed();
	}

	@Action
	public void allinAction() {
		ClientController.getInstance().allinButtonPressed();
	}

	public void enableGameActionButtons(boolean[] enabledActions) {
		this.checkButton.setEnabled(enabledActions[3]);
		this.callButton.setEnabled(enabledActions[4]);
		this.betTextField.setEnabled(enabledActions[5]);
		this.betButton.setEnabled(enabledActions[5]);
		enableRaiseButton(enabledActions[6]);
		this.allinButton.setEnabled(enabledActions[7]);
		this.foldButton.setEnabled(enabledActions[8]);
	}

	private void enableRaiseButton(boolean enable) {
		if (enable) {
			betButton.setText("Raise");
		} else {
			betButton.setText("Bet");
		}
	}

}
